package com.loe.ktui.custom

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.core.content.res.getBooleanOrThrow
import androidx.core.content.res.getDimensionOrThrow
import androidx.core.content.res.getResourceIdOrThrow
import com.loe.ktui.*
import com.loe.ktui.util.KtCornerProvider
import com.loe.ktui.util.UiShape

open class UiToggleImage(id: String? = null) : UiImage(id)
{
    var checkImage0: Any? = null
    var checkImage1: Any? = null
    var checkImage2: Any? = null

    var isEnable: Boolean? = null
    var isChecked: Boolean = false

    var onCheck: (KtToggleImage.(isCheck: Boolean) -> Unit)? = null

    open fun initUI(context: Context, view: KtToggleImage)
    {
        super.initUI(context, view)

        checkImage0?.let { view.checkImage0 = it }
        checkImage1?.let { view.checkImage1 = it }
        checkImage2?.let { view.checkImage2 = it }

        view.onCheck = null
        isEnable?.let { view.isEnable = it }
        view.isChecked = isChecked
        onCheck?.let { onCheck -> view.setOnCheck { view.onCheck(it) } }
    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtToggleImage
    {
        return KtToggleImage(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_ToggleImage(id: String? = null, block: (UiToggleImage.() -> Unit)? = null): UiToggleImage
{
    val ui = UiToggleImage(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

open class KtToggleImage : KtImageView
{
    constructor(context: Context) : super(context)
    {
        initXML()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtToggleImage))
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtToggleImage, defStyleAttr, 0))
    }

    private fun initXML(typedArray: TypedArray? = null)
    {
        setOnClickListener()
        {
            if (isEnable)
            {
                toggle()
            }
        }
        typedArray?.let()
        {
            if (it.hasValue(R.styleable.KtToggleImage_kt_corner))
            {
                val corner = it.getDimensionOrThrow(R.styleable.KtToggleImage_kt_corner).toInt()
                val cornerDirect = KtDirects[it.getInt(R.styleable.KtToggleImage_kt_cornerDirect, 4)]

                outlineProvider = KtCornerProvider(corner, cornerDirect, this)
            }
            if (it.hasValue(R.styleable.KtToggleImage_kt_isAlphaClick))
            {
                isAlphaClick = it.getBooleanOrThrow(R.styleable.KtToggleImage_kt_isAlphaClick)
            }

            if (it.hasValue(R.styleable.KtToggleImage_kt_check_image0))
            {
                checkImage0 = it.getResourceIdOrThrow(R.styleable.KtToggleImage_kt_check_image0)
            }
            if (it.hasValue(R.styleable.KtToggleImage_kt_check_image1))
            {
                checkImage1 = it.getResourceIdOrThrow(R.styleable.KtToggleImage_kt_check_image1)
            }
            if (it.hasValue(R.styleable.KtToggleImage_kt_check_image2))
            {
                checkImage2 = it.getResourceIdOrThrow(R.styleable.KtToggleImage_kt_check_image2)
            }

            if (it.hasValue(R.styleable.KtToggleImage_kt_isEnable))
            {
                isEnable = it.getBooleanOrThrow(R.styleable.KtToggleImage_kt_isEnable)
            }

            isChecked = it.getBoolean(R.styleable.KtToggleImage_kt_isChecked, false)

            it.recycle()
        }
    }

    /********************************************************************************************/

    internal var checkImage0: Any? = null
    internal var checkImage1: Any? = null
    internal var checkImage2: Any? = null

    fun toggle()
    {
        isChecked = !isChecked
    }

    fun notifyCheck()
    {
        if (isEnable)
        {
            if (isChecked)
            {
                setNowImage(checkImage1)
            } else
            {

                setNowImage(checkImage0)
            }
        } else
        {
            if (isChecked)
            {
                setNowImage(checkImage2)
            } else
            {

                setNowImage(checkImage0)
            }
        }
    }

    private fun setNowImage(img: Any?)
    {
        if (img is Int)
        {
            setImageResource(img)
        } else if (img is UiShape)
        {
            setImageDrawable(img.createDrawable())
        } else
        {
            context.getUrlDrawable(img.toString()) { setImageDrawable(it) }
        }
    }

    var isEnable = true
        set(value)
        {
            field = value
            notifyCheck()
        }

    var isChecked = false
        set(value)
        {
            field = value
            notifyCheck()
            if(!ignoreOnCheck) onCheck?.invoke(isChecked)
        }

    private var ignoreOnCheck = false

    fun setIgnoreCheck(isCheck: Boolean, on: ((Boolean)-> Unit)? = null)
    {
        ignoreOnCheck = true
        this.isChecked = isCheck
        ignoreOnCheck = false
        on?.invoke(isCheck)
    }

    internal var onCheck: ((isCheck: Boolean) -> Unit)? = null

    fun setOnCheck(onCheck: (isCheck: Boolean) -> Unit)
    {
        this.onCheck = onCheck
    }
}